package com.zhuiyi.springbootmybatis.controller;

import com.zhuiyi.springbootmybatis.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//别用RestController 返回json对象，不是页面
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {

    @RequestMapping("/usemodelmap")
    public String show(ModelMap modelMap) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "张三");
        map.put("age", 23);
        map.put("address", "内江");
        modelMap.addAllAttributes(map);
//自动到templates目录下找HTML页面
        return "/thymeleaf";
    }

    @RequestMapping("/mv")
    public ModelAndView useModelAndView() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/thymeleaf.html");
        mv.addObject("name", "张三");
        mv.addObject("score", "99");
        return mv;
    }

    /**
     * if unless使用
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/if")
    public String useIfUnless(ModelMap modelMap) {
        modelMap.addAttribute("isOK", false);
        modelMap.addAttribute("name", "李四");
        return "/thymeleaf";
    }

    /**
     * 实体传参
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/shiti")
    public String useShiTiChuanCan(ModelMap modelMap) {
        modelMap.addAttribute("user", new User("李义", "123456"));
        return "/thymeleaf";
    }

    /**
     * 遍历列表
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("/list")
    public String userList(ModelMap modelMap) {
        List<User> userList = new ArrayList<User>();
        userList.add(new User("张三", "123"));
        userList.add(new User("李四", "144"));
        userList.add(new User("66", "155"));
        modelMap.addAttribute("userList", userList);
        return "/thymeleaf";
    }

    /**
     * spirng EL表达式
     *
     * @param session
     * @return
     */
    @RequestMapping("/el")
    public String testEl(HttpSession session) {
        User user = new User("张思", "123");
        session.setAttribute("user", user);
        return "/thymeleaf";
    }

    @RequestMapping("/url")
    public String url(ModelMap map) {

        Map<String, Object> data = new HashMap<>();
        data.put("url", "http://localhost:8080/order/dtails");
        data.put("orderId", "1001");    //
        map.addAllAttributes(data);
        return "/thymeleaf";
    }

    /**
     * 公共样式使用
     * @return
     */
    @RequestMapping("/commontest")
    public String testCommons(){
        return "/usercommonTest.html";
    }

    /**
     * 跳转到登录页面
     * @return
     */
    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }

}
